package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.Teacher;
import cn.kgc.springboot.service.TeacherService;
import cn.kgc.springboot.uitl.LayTable;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/7/25
 */
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //添加教师信息
    @PostMapping
    public Teacher add(@RequestBody Teacher teacher){
        return  teacherService.addTeacher(teacher);
    }


    //根据id 和专业 删除教师信息
    @DeleteMapping("{id}/{pro}")
    public Boolean deleteTeacher(@PathVariable Integer id, @PathVariable String pro){

        // 创建条件构造器对象 用于设置删除的 where条件  where   id = ？  and  profession = ？
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("id", id).eq("profession", pro);
        return teacherService.remove(teacherQueryWrapper);
    }

    @GetMapping("{name}/{pro}")
    public List<Teacher> selectTeacherByNameAndPro(@PathVariable String name,@PathVariable String pro){

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("tea_name", name).eq("profession", pro);
        return teacherService.list(teacherQueryWrapper);

    }

    //查询所有教师信息
    @GetMapping
    public List<Teacher> getAll(){
        return teacherService.list();
    }

    @GetMapping("page/{page}/{limit}")
    public LayTable getByPage(@PathVariable Integer page, @PathVariable Integer limit){

        Page<Teacher> teacherPage = new Page<>(page, limit);
        teacherService.page(teacherPage);

        return new LayTable().setCode(200).setMsg("分页查询成功").setCount(teacherPage.getTotal()).setData(teacherPage.getRecords());
    }

    @PutMapping
    public Boolean updateTeacher(@RequestBody Teacher teacher){
      return  teacherService.updateById(teacher);
    }


}
